<?php
class company extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的


		$this->tit = '公司';
		$this->modelObj = D('company');
		$this->adminObj = D('company_admin');

		$this->cityObj = D('sys_city');
		$this->cateObj = D('sys_cate');

		$this->data_list = $this->cityObj->get_TreeList("status=1 and id IN ({$this->site['city_ids']})");

	}
    
    //列表
	public function index(){
		$this->tabtit = array('全部','回收站');
		$cond = "1=1".$this->cond_base;
		//加入查询条件
		switch($this->tab){
			case '1':
				$cond .=" and status=0";
				break;
			default:
				$cond .=" and status=1";
		}
		$result = $this->modelObj->get_pagelist($cond);
		// dump($result);exit;
		if($result){
			$this->areas = $this->cityObj->ids($result['list'],'area_id');
			// dump($this->areas);exit;
			$this->list = $result['list'];
			$this->showPage = $result['showPage'];
			$this->total = $result['total'];
		}
		
	}

	//添加
	public function add(){
		if($this->isAjax()){
			$data = $this->postData('data');
			$res = $this->adminObj->check_user($data['username']);
			if($res){
				$this->renderError('用户名已存在');
			}
			$data['site_id'] = $this->site['id'];
			$res = $this->modelObj->add_site($data);
			$this->renderSuccess('添加成功', "/{$this->routeUrl}/index");
		}
		$this->info = array('sort'=>100,'status'=>1);
		$this->display($this->routeUrl."/set.php");
	}

	//编辑
	public function edit(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');
			$this->modelObj->edit_site($id,$data);
			$this->renderSuccess('编辑成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->modelObj->get_site($id);
		// dump($this->info);exit;
		$this->display($this->routeUrl."/set.php");
	}

    //隐藏
	public function ban(){
		$id = arg('id');
		if($this->isAjax()){
			$res = $this->modelObj->act($id,0);
			$this->renderSuccess('隐藏成功');
		}
	}

	//显示
	public function act(){
		$id = arg('id');
		$status = arg('status');
		if($status==1){
			$msg = '显示';
		}else{
			$msg = '隐藏';
		}
		if($this->isAjax()){
			$res = $this->modelObj->act($id,$status);
			$this->renderSuccess($msg.'成功');
		}
	}

    //删除
	public function del(){
		$id = arg('id');
		if($this->isAjax()){
			$res = $this->modelObj->del($id);
			$this->renderSuccess('删除成功');
		}
	}

	//管理
	public function login(){
		$id = arg('id');
		$info = $this->modelObj->get_site($id);
		$user = $info['admin'];

		$_SESSION['company_admin']['id'] = $user['id'];
        $_SESSION['logintime'] = time();
        // set_cookie('company_admin_mnm',$user['username'],time()+3600*24);
		
		$this->redirect('/company/main/index');
	}

	//编辑
	public function auth(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');
			
			$arrBase['city_ids'] = joinStr($data['city']);
			$arrBase['cate_ids'] = joinStr($data['cate']);

			$this->modelObj->edit($id,$arrBase);
			$this->renderSuccess('授权成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->modelObj->findById($id);

		$cond_city = "status=1 and id IN ({$this->site['city_ids']})";
		$cond_cate = "status=1 and id IN ({$this->site['cate_ids']})";
		$cond_zone = "status=1 and id IN ({$this->site['zone_ids']})";
		$this->areaList = $this->cityObj->get_JsTree($cond_city,$this->info['city_ids']);
		$this->cateList = $this->cateObj->get_JsTree($cond_cate,$this->info['cate_ids']);
	}

}